package com.sg.hms.controller;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.sg.hms.Model.Application;
import com.sg.hms.Model.Pagebean;
import com.sg.hms.Model.User;
import com.sg.hms.Service.ApplicationService;
import com.sg.hms.ServiceImpl.ApplicationServiceImpl;
/***
 * 通过关键字查看申请关键字为空则返回所有申请
 * @author 吴磊
 *
 */
public class SearchApplicationController extends HttpServlet{
	ApplicationService app=new ApplicationServiceImpl();
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(req, resp);
	}
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		User user=(User) req.getSession().getAttribute("currentuser");
		int temp=user.getuId();
		req.setCharacterEncoding("UTF-8");
		String key=req.getParameter("key");
		String pString = req.getParameter("page");
		int p = 0;
		if (pString == null) {
			p = 1;
		} else {
			p = Integer.parseInt(pString);
		}
		Pagebean pagebean=new Pagebean(p, 5);
		if ((key==null)==false) {
			req.getSession().setAttribute("akey", key);
		}
		String ak=(String) req.getSession().getAttribute("akey");
		ArrayList<Application> all=app.searchallApplication(user.getuId(), ak);
		int size=all.size();
		if (size<=5) {
			req.setAttribute("pag1", null);
		}
		else {
			req.setAttribute("page1", pagebean.getthisPage5(size,p,pagebean.getPageSize()));
		}
		ArrayList<Application> applications=app.searchApplication(user.getuId(), ak, pagebean);
		req.setAttribute("applications", applications);
		req.getRequestDispatcher("page/demand.jsp").forward(req, resp);
	}

}
